﻿namespace Print_Calendar
{
    internal class Program
    {
        //设置表格常数
        const int ColumnWidth = 40;
        const int blankWidth = 10;
        const int CharacterWidth = 4;
        const int RowHeight = 10;
        /// <summary>
        /// 打印2022年的日历
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int x = 0, y = 0;
            DateTime FirstDay = new DateTime(2022, 1, 1);
            DateTime IndexDate = FirstDay;
            do
            {
                //定位初始位置
                Console.SetCursorPosition(x, y);
                //打印表头
                Console.SetCursorPosition(x + blankWidth, y);
                Console.Write(IndexDate.Month + "月");
                //打印星期
                y++;
                Console.SetCursorPosition(x, y);
                Console.Write("日  一  二  三  四  五  六");
                y++;
                //打印日期
                do
                {
                    //打印一个月的第一天
                    if (IndexDate.Day == 1)
                    {
                        x += Convert.ToInt32(IndexDate.DayOfWeek) * CharacterWidth;
                        Console.SetCursorPosition(x, y);
                        Console.Write(IndexDate.Day);
                    }
                    //打印一个月中的其他天数
                    else
                    {
                        //打印周日
                        if (Convert.ToInt32(IndexDate.DayOfWeek) == 0)
                        {
                            if(x >= ColumnWidth)
                                x -= 24;
                            else 
                                x = 0;
                            y++;
                            Console.SetCursorPosition(x, y);
                            Console.Write(IndexDate.Day);
                        }
                        else
                        {
                            x += CharacterWidth;
                            Console.SetCursorPosition(x, y);
                            Console.Write(IndexDate.Day);
                        }
                    }
                    IndexDate = IndexDate.AddDays(1);
                }while (IndexDate.Day != 1 );
                //按月份控制光标位置
                if(IndexDate.Month % 3 == 0)
                {
                    x = ColumnWidth * 2;
                    y = RowHeight * ((IndexDate.Month - 1) / 3 );
                }
                else
                {
                    x = ColumnWidth *(IndexDate.Month % 3 - 1);
                    y = RowHeight * (IndexDate.Month / 3);
                }
             //年份变为为2023时结束程序
            } while (IndexDate.Year == 2022);
            Console.ReadKey();
        }
    }
}